package unicall.em.tags;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.data.domain.Page;

public class Pagination extends TagSupport {

    private static final long serialVersionUID = -1003163083722311253L;

    private Page<?> page;
    
    private String url;
    
        public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Page<?> getPage() {
        return page;
    }

    public void setPage(Page<?> page) {
        this.page = page;
    }

    @Override
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }

    @Override
    public int doStartTag() throws JspException {
        JspWriter out = this.pageContext.getOut();
        try {
            if(null==page){
                out.println("No Page Found");
                return SKIP_BODY;
            }
           StringBuffer sb = new StringBuffer();
           sb.append("<form name='_pageFrom' action='"+this.url+"' method='GET' class='_pageForm'></input type='hidden' name='pageSize' value=20/><input type='hidden' name='pageNumber' value='"+page.getNumber()+"'/></form>");
           sb.append("<div class='paging_bj clear'><div class='nums_bj'>");
           sb.append("<p class='nums_infor'>当前第<em>"+(page.getNumber()+1)+"</em>页/每页20条记录（共<i>"+page.getTotalElements()+"</i>条）</p></div>");
           sb.append("<span class='paging'>");
           if(!page.hasPrevious()){
               sb.append("<a class='pagAll_l' title='首页'></a><a class='pag_l' title='上一页'></a>");
           }else{
               sb.append("<a class='pagAll_lH' data-func='pag_link' title='首页' data-page='0'></a><a class='pag_lH' data-func='pag_link' title='上一页' data-page='"+(page.isFirst()?0:page.getNumber())+"'></a>");
           }
           if(!page.hasNext()){
               sb.append("<a class='pag_r'  title='下一页'></a><a class='pagAll_r' title='末页'></a></span></div>");
           }else{
               sb.append("<a class='pag_rH' data-func='pag_link' title='下一页' data-page='"+(page.isLast()?page.getNumber():page.getNumber()+1)+"'></a><a class='pagAll_rH' data-func='pag_link' title='末页' data-page='"+(page.getTotalPages()-1)+"'></a></span></div>");
           }
           out.println(sb.toString());
            
        } catch (Exception e) {
           throw new JspException(e.getMessage());
        }
        return SKIP_BODY;
    }

    @Override
    public void release() {
        super.release();
        this.page = null;
    }
   
    
    

}
